package com.dqhl.business.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.AddPromotion;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by guanluocang on 2016/11/9.
 */
public class AddPromotionSingleGridViewAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<AddPromotion> addPromotionList;
    private int selectedPosition = 0;

    public AddPromotionSingleGridViewAdapter(Context context, List<AddPromotion> addPromotionList) {
        this.context = context;
        this.addPromotionList = addPromotionList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return addPromotionList.size();
    }

    @Override
    public Object getItem(int i) {
        return addPromotionList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void clearSelection(int position) {
        selectedPosition = position;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_addpromotion_single, viewGroup, false);
        }
        TextView tv_goods_name = ViewHolder.get(view, R.id.tv_goods_name);
        ImageView iv_promotion = ViewHolder.get(view,R.id.iv_promotion);
        AddPromotion addPromotion = addPromotionList.get(i);
        tv_goods_name.setText(addPromotion.getName());
        x.image().bind(iv_promotion, Config.img_url+addPromotion.getPic_a());
        if (selectedPosition == i) {
            tv_goods_name.setTextColor(ContextCompat.getColor(context, R.color.daiqianshou));
            tv_goods_name.setBackgroundResource(R.color.main_textContent);
        } else {
            tv_goods_name.setTextColor(ContextCompat.getColor(context, R.color.main_textColor));
            tv_goods_name.setBackgroundResource(R.color.white);
        }
        return view;
    }
}
